{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting paddlex==2.1.0\n",
      "  Using cached https://files.pythonhosted.org/packages/ca/03/b401c6a34685aa698e7c2fbcfad029892cbfa4b562eaaa7722037fef86ed/paddlex-2.1.0-py3-none-any.whl\n",
      "Requirement already satisfied: chardet in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from paddlex==2.1.0) (3.0.4)\n",
      "Collecting lap (from paddlex==2.1.0)\n",
      "  Downloading https://files.pythonhosted.org/packages/b9/28/1cbfb0ad73a91fa6ef1e118488f1654b9fd8b14b943f0763c4d911a50d9c/lap-0.5.12-cp37-cp37m-win_amd64.whl (1.5MB)\n",
      "Collecting pycocotools (from paddlex==2.1.0)\n",
      "  Using cached https://files.pythonhosted.org/packages/6f/1e/35838eb5786533e45dfd5d3607e231eca22e196dc410d466010b56918b35/pycocotools-2.0.7-cp37-cp37m-win_amd64.whl\n",
      "Requirement already satisfied: openpyxl in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from paddlex==2.1.0) (2.5.6)\n",
      "Requirement already satisfied: flask-cors in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from paddlex==2.1.0) (3.0.6)\n",
      "Collecting paddleslim==2.2.1 (from paddlex==2.1.0)\n",
      "  Using cached https://files.pythonhosted.org/packages/0b/dc/f46c4669d4cb35de23581a2380d55bf9d38bb6855aab1978fdb956d85da6/paddleslim-2.2.1-py3-none-any.whl\n",
      "Requirement already satisfied: scipy in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from paddlex==2.1.0) (1.1.0)\n",
      "Requirement already satisfied: tqdm in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from paddlex==2.1.0) (4.26.0)\n",
      "Collecting motmetrics (from paddlex==2.1.0)\n",
      "  Using cached https://files.pythonhosted.org/packages/2f/d9/7b77e1e2db80b6f8133065ffbccdaa3c911df5f95a7af30829fcaa10a3d7/motmetrics-1.4.0-py3-none-any.whl\n",
      "Collecting scikit-learn==0.23.2 (from paddlex==2.1.0)\n",
      "  Using cached https://files.pythonhosted.org/packages/92/db/8c50996186faed765392cb5ba495e8764643b71adbd168535baf0fcae5f1/scikit_learn-0.23.2-cp37-cp37m-win_amd64.whl\n",
      "Collecting visualdl>=2.2.2 (from paddlex==2.1.0)\n",
      "  Using cached https://files.pythonhosted.org/packages/ea/b5/37726c750a4f4598660998327c3566b2d2ed5a1a5f44e9f0dde875602447/visualdl-2.5.3-py3-none-any.whl\n",
      "Collecting opencv-python (from paddlex==2.1.0)\n",
      "  Using cached https://files.pythonhosted.org/packages/17/06/68c27a523103dad5837dc5b87e71285280c4f098c60e4fe8a8db6486ab09/opencv-python-4.11.0.86.tar.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  Missing build time requirements in pyproject.toml for opencv-python from https://files.pythonhosted.org/packages/17/06/68c27a523103dad5837dc5b87e71285280c4f098c60e4fe8a8db6486ab09/opencv-python-4.11.0.86.tar.gz#sha256=03d60ccae62304860d232272e4a4fda93c39d595780cb40b161b310244b736a4 (from paddlex==2.1.0): 'wheel'.\n",
      "  This version of pip does not implement PEP 517 so it cannot build a wheel without 'setuptools' and 'wheel'.\n",
      "  Could not find a version that satisfies the requirement numpy==1.13.3 (from versions: 1.14.5, 1.14.6, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 1.15.4, 1.16.0, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, 1.16.6, 1.17.0, 1.17.1, 1.17.2, 1.17.3, 1.17.4, 1.17.5, 1.18.0, 1.18.1, 1.18.2, 1.18.3, 1.18.4, 1.18.5, 1.19.0, 1.19.1, 1.19.2, 1.19.3, 1.19.4, 1.19.5, 1.20.0, 1.20.1, 1.20.2, 1.20.3, 1.21.0, 1.21.1, 1.21.2, 1.21.3, 1.21.4, 1.21.5, 1.21.6)\n",
      "No matching distribution found for numpy==1.13.3\n",
      "You are using pip version 10.0.1, however version 24.0 is available.\n",
      "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install paddlex==2.1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Normalize' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-4135722bc391>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m                                    transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,\n\u001b[0;32m      6\u001b[0m                                                             contrast_range=0.9,contrast_prob=0.5,saturation_range=0.9,saturation_prob=0.5,hue_range=18,hue_prob=0.5),\n\u001b[1;32m----> 7\u001b[1;33m                                     transforms,Normalize()])\n\u001b[0m\u001b[0;32m      8\u001b[0m val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n\u001b[0;32m      9\u001b[0m                                   \u001b[0mtransforms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mCenterCrop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcrop_size\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m224\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'Normalize' is not defined"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "from paddlex import transforms\n",
    "train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),\n",
    "                                    transforms.RandomHorizontalFlip(),\n",
    "                                   transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,\n",
    "                                                            contrast_range=0.9,contrast_prob=0.5,saturation_range=0.9,saturation_prob=0.5,hue_range=18,hue_prob=0.5),\n",
    "                                    transforms,Normalize()])\n",
    "val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n",
    "                                  transforms.CenterCrop(crop_size=224),\n",
    "                                  transfroms.Normalize()])\n",
    "train_dataset=pdx.datasets.lmageNet(\n",
    "data_dir='garbage',\n",
    "file_list='./train.txt',\n",
    "label_list='./labels.txt',\n",
    "tranasfroms=train_transforms,\n",
    "shuffle=True)\n",
    "val_dataset=pdx.datasets.lmageNet(\n",
    "data_dir='./garbage',\n",
    "file_list='./val.txt',\n",
    "label_list='./labels.txt',\n",
    "transforms=val_tansforms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddlex as pdx\n",
    "num_classes=len(train_dataset.labels)\n",
    "model=pdx.cls.ResNet50_vd_ssld(num_classes=num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.train(num_epoch=5,\n",
    "train_dataset=train_dataset,\n",
    "train_batch_size=16,\n",
    "eval_dataset=val_dataset,\n",
    "lr_decay_epochs=[80,100,150],\n",
    "save_interval_epochs=1,\n",
    "learning_rate=0.002,\n",
    "save_dir='output/ResNet50_vd_ssid',\n",
    "use_vdl=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model=pdx.load_model('output/ResNet50_vd_ssld/best_model')\n",
    "image_name='./garbage/paper/paper10.jpg'\n",
    "result=model.predict(mage_name)\n",
    "print('Predict Result:',result)\n",
    "number=result[0]['category']\n",
    "number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[INFO]Model[ResNet50_vd_ssld]loaded.\n",
    "Predict Result:[{'category_id':0,'category':'paper','score':0.9847607}]\n",
    "    'paper'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
